package com.uenpay.im.weight;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import com.qyx.android.weight.datepicker.wheelview.OnWheelScrollListener;
import com.qyx.android.weight.datepicker.wheelview.WheelView;
import com.qyx.android.weight.datepicker.wheelview.adapter.NumericWheelAdapter;
import com.uenpay.im.R;
import com.uenpay.im.callback.IDateDailogListener;

import java.util.Calendar;

/**
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * 
 */
public class MyDateDialog {
	private LayoutInflater inflater = null;
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private WheelView hour;
	private WheelView minute;
	private Context mContext;
	private int mYear = Calendar.YEAR;
	private int mMonth = 0;
	private int mDay = 1;
	private int mMinute = 0;
	private int mHour = 0;
	private IDateDailogListener mDailogListener;
	private Dialog dialog = null;
	private int minYear = 2016;
	private int selectedYear = -1;
	private int selectedMonth = -1;
	private int selectedDay = -1;
	private int selectedMinute = -1;
	private int selectedHour = -1;
	private boolean is_show_hour = false;
	private boolean is_year = true;

	/**
	 * 
	 * @param _selectedYear
	 * @param _selectedMonth
	 * @param _selectedDay
	 * @param _Context
	 * @param _is_show_hour
	 * @param _is_year
	 * @param _DailogListener
	 */
	public MyDateDialog(int _selectedYear, int _selectedMonth,
			int _selectedDay, int _selectedHour, int _selectedMinute,
			Context _Context, boolean _is_show_hour, boolean _is_year,
			IDateDailogListener _DailogListener) {
		inflater = LayoutInflater.from(_Context);
		mContext = _Context;
		mDailogListener = _DailogListener;
		selectedYear = _selectedYear;
		selectedMonth = _selectedMonth;
		selectedDay = _selectedDay;
		selectedHour = _selectedHour;
		selectedMinute = _selectedMinute;
		is_show_hour = _is_show_hour;
		is_year = _is_year;
	}

	public void showDialog() {
		dialog = new Dialog(mContext, R.style.pop_dialog);
		Window win = dialog.getWindow();
		win.setGravity(Gravity.BOTTOM);
		win.setWindowAnimations(R.style.dialogWindowAnim);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		win.setLayout(android.view.ViewGroup.LayoutParams.MATCH_PARENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

		dialog.setContentView(getDataPick());
	}

	private View getDataPick() {
		Calendar c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR) + 5;
		int curYear = mYear;
		int curMonth = mMonth + 1;
		View view = inflater.inflate(R.layout.wheel_date_picker, null);
		year = (WheelView) view.findViewById(R.id.year);
		NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(
				mContext, minYear, norYear);
		numericWheelAdapter1.setLabel(mContext.getResources().getString(
				R.string.year));
		year.setViewAdapter(numericWheelAdapter1);
		year.setCyclic(true);
		year.addScrollingListener(scrollListener);

		month = (WheelView) view.findViewById(R.id.month);
		NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(
				mContext, 1, 12, "%02d");
		numericWheelAdapter2.setLabel(mContext.getResources().getString(
				R.string.month));
		month.setViewAdapter(numericWheelAdapter2);
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);

		day = (WheelView) view.findViewById(R.id.day);
		initDay(curYear, curMonth);
		day.setCyclic(true);
		// ����ʾ������
		if (!is_year) {
			year.setVisibility(View.GONE);
			month.setVisibility(View.GONE);
			day.setVisibility(View.GONE);
		}
		minute = (WheelView) view.findViewById(R.id.minute);
		if (is_show_hour) {
			minute.setVisibility(View.VISIBLE);
		}
		initMinute();
		minute.setCyclic(true);
		minute.addScrollingListener(scrollListener);

		hour = (WheelView) view.findViewById(R.id.hour);
		if (is_show_hour) {
			hour.setVisibility(View.VISIBLE);
		}
		initHour();
		hour.setCyclic(true);
		hour.addScrollingListener(scrollListener);

		year.setVisibleItems(7);
		month.setVisibleItems(7);
		day.setVisibleItems(7);
		minute.setVisibleItems(7);
		hour.setVisibleItems(7);

		if (selectedDay == -1 || selectedMonth == -1 || selectedYear == -1) {
			year.setCurrentItem(c.get(Calendar.YEAR) - minYear);
			month.setCurrentItem(c.get(Calendar.MONDAY));
			day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH) - 1);
		} else {
			year.setCurrentItem(selectedYear - minYear);
			month.setCurrentItem(selectedMonth);
			day.setCurrentItem(selectedDay - 1);
		}
		if (selectedHour == -1 || selectedMinute == -1) {
			hour.setCurrentItem(c.get(Calendar.HOUR_OF_DAY));
			minute.setCurrentItem(c.get(Calendar.MINUTE));
		} else {
			hour.setCurrentItem(selectedHour);
			minute.setCurrentItem(selectedMinute);
		}

		day.addScrollingListener(scrollListener);
		view.findViewById(R.id.sure_btn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						mDailogListener.OnDailogChick(
								String.format("%02d", mYear),
								String.format("%02d", mMonth),
								String.format("%02d", mDay),
								String.format("%02d", mHour),
								String.format("%02d", mMinute));
						if (dialog != null) {
							dialog.dismiss();
						}
					}
				});
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONDAY) + 1;
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
		return view;
	}

	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(WheelView wheel) {

		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			int n_year = year.getCurrentItem() + minYear;
			int n_month = month.getCurrentItem() + 1;
			initDay(n_year, n_month);
			mDay = day.getCurrentItem() + 1;
			mYear = n_year;
			mMonth = n_month;
			mMinute = minute.getCurrentItem();
			mHour = hour.getCurrentItem();
		}
	};

	private void initDay(int arg1, int arg2) {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
				mContext, 1, getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel(mContext.getResources().getString(
				R.string.day));
		day.setViewAdapter(numericWheelAdapter);
	}

	private void initHour() {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
				mContext, 0, 23, "%02d");
		numericWheelAdapter.setLabel(mContext.getResources().getString(
				R.string.hour));
		hour.setViewAdapter(numericWheelAdapter);
	}

	private void initMinute() {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
				mContext, 0, 59, "%02d");
		numericWheelAdapter.setLabel(mContext.getResources().getString(
				R.string.minute));
		minute.setViewAdapter(numericWheelAdapter);
	}

	/**
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}

}
